package plus.easydo.server.user.mp.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import plus.easydo.core.result.R;
import plus.easydo.server.user.mp.constant.UserConstants;
import plus.easydo.server.user.mp.entity.SysDictType;
import plus.easydo.server.user.mp.qo.SysDictTypeQo;
import plus.easydo.server.user.mp.service.ISysDictTypeService;
import plus.easydo.starter.log.annotation.SystemLog;
import plus.easydo.starter.mybatis.plus.base.MPBaseController;
import plus.easydo.starter.mybatis.plus.result.MPDataResult;
import plus.easydo.starter.oauth.core.utils.Oauth2Utils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 数据字典信息
 *
 * @author ruoyi
 */
@Api(tags = "数据字典信息")
@RestController
@RequestMapping("/dict/type")
public class SysDictTypeController extends MPBaseController {

    @Resource
    private ISysDictTypeService dictTypeService;


    @PreAuthorize("hasPermission('system:dict:list')")
    @GetMapping("/list")
    public MPDataResult list(SysDictTypeQo sysDictTypeQo) {
        Page<SysDictType> page = dictTypeService.selectDictTypeList(sysDictTypeQo);
        return ok(page);
    }

//    @PreAuthorize("hasPermission('system:dict:export")
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, SysDictType dictType) throws IOException
//    {
//        List<SysDictType> list = dictTypeService.selectDictTypeList(dictType);
//        ExcelUtil<SysDictType> util = new ExcelUtil<SysDictType>(SysDictType.class);
//        util.exportExcel(response, list, "字典类型");
//    }

    /**
     * 查询字典类型详细
     */
    @PreAuthorize("hasPermission('system:dict:query')")
    @GetMapping(value = "/{dictId}")
    public R<SysDictType> getInfo(@PathVariable Long dictId) {
        return ok(dictTypeService.selectDictTypeById(dictId));
    }

    /**
     * 新增字典类型
     */
    @SystemLog(value = "新增字典类型",saveLog = true)
    @PreAuthorize("hasPermission('system:dict:add')")
    @PostMapping
    public R<Object> add(@Validated @RequestBody SysDictType dict) {
        if (UserConstants.NOT_UNIQUE.equals(dictTypeService.checkDictTypeUnique(dict))) {
            return fail("新增字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        dict.setCreateBy(Oauth2Utils.getUserName());
        return opResult(dictTypeService.insertDictType(dict));
    }

    /**
     * 修改字典类型
     */
    @SystemLog(value = "修改字典类型",saveLog = true)
    @PreAuthorize("hasPermission('system:dict:edit')")
    @PutMapping
    public R<Object> edit(@Validated @RequestBody SysDictType dict) {
        if (UserConstants.NOT_UNIQUE.equals(dictTypeService.checkDictTypeUnique(dict))) {
            return fail("修改字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        dict.setUpdateBy(Oauth2Utils.getUserName());
        return opResult(dictTypeService.updateDictType(dict));
    }

    /**
     * 删除字典类型
     */
    @SystemLog(value = "删除字典类型",saveLog = true)
    @PreAuthorize("hasPermission('system:dict:remove')")
    @DeleteMapping("/{dictIds}")
    public R<Object> remove(@PathVariable Long[] dictIds) {
        return opResult(dictTypeService.deleteDictTypeByIds(dictIds));
    }

    /**
     * 清空缓存
     */
    @SystemLog(value = "清空字典缓存",saveLog = true)
    @PreAuthorize("hasPermission('system:dict:remove')")
    @DeleteMapping("/clearCache")
    public R<Object> clearCache() {
        dictTypeService.clearCache();
        return ok();
    }

    /**
     * 获取字典选择框列表
     */
    @GetMapping("/optionselect")
    public R<List<SysDictType>> optionselect() {
        List<SysDictType> dictTypes = dictTypeService.selectDictTypeAll();
        return ok(dictTypes);
    }
}
